/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.study.cation;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.study.dao.EnrollmentDAO;
import com.study.dao.OrderDAO;
import com.study.dao.StudentDAO;
import com.study.dto.EnrollmentDTO;
import com.study.dto.Order_DetailDTO;
import com.study.dto.StudentDTO;

/**
 *
 * @author Truong Anh Tuan
 */
public class LoginAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String ERR = "err";
    private static final String ADMIN = "admin";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("studentId");
        if (!id.equals("admin")) {
            StudentDAO dao = new StudentDAO();
            OrderDAO ordao = new OrderDAO();
            ArrayList<Order_DetailDTO> list = ordao.Read_By_Student_Id(id);
            ArrayList<StudentDTO> dto = dao.ReadByStudentsId(id);
            if (dto != null) {
                HttpSession session = request.getSession(true);
                session.putValue("id", id);
                request.setAttribute("st", dto);
                request.setAttribute("list", list);
                return mapping.findForward(SUCCESS);
            }
            return mapping.findForward(ERR);
        }else{
            EnrollmentDAO endao = new EnrollmentDAO();
            ArrayList<EnrollmentDTO> enlist = endao.ReadByAll();
            request.setAttribute("en", enlist);
            return mapping.findForward(ADMIN);
        }
    }
}
